package androidx.test.orchestrator.listeners;

import android.app.Instrumentation;
import android.os.Bundle;
import android.util.Log;
import androidx.test.orchestrator.junit.BundleJUnitUtils;
import androidx.test.orchestrator.junit.ParcelableDescription;
import androidx.test.orchestrator.junit.ParcelableFailure;
import java.util.ArrayList;
import java.util.List;

/* loaded from: classes.dex */
public final class OrchestrationListenerManager {
    public static final String KEY_TEST_EVENT = "TestEvent";
    private static final String TAG = "ListenerManager";
    private final Instrumentation instrumentation;
    private ParcelableDescription lastDescription;
    private final List<OrchestrationRunListener> listeners = new ArrayList();
    private boolean markTerminationAsFailure = false;

    /* loaded from: classes.dex */
    public enum TestEvent {
        TEST_RUN_STARTED,
        TEST_RUN_FINISHED,
        TEST_STARTED,
        TEST_FINISHED,
        TEST_FAILURE,
        TEST_ASSUMPTION_FAILURE,
        TEST_IGNORED
    }

    public OrchestrationListenerManager(Instrumentation instrumentation) {
        if (instrumentation == null) {
            throw new IllegalArgumentException("Instrumentation must not be null");
        }
        this.instrumentation = instrumentation;
    }

    public void addListener(OrchestrationRunListener listener) {
        listener.setInstrumentation(this.instrumentation);
        this.listeners.add(listener);
    }

    public void orchestrationRunStarted(int testCount) {
        for (OrchestrationRunListener orchestrationRunListener : this.listeners) {
            orchestrationRunListener.orchestrationRunStarted(testCount);
        }
    }

    public void testProcessStarted(ParcelableDescription description) {
        this.lastDescription = description;
        this.markTerminationAsFailure = true;
    }

    public void testProcessFinished(String outputFile) {
        if (this.markTerminationAsFailure) {
            for (OrchestrationRunListener orchestrationRunListener : this.listeners) {
                ParcelableDescription parcelableDescription = this.lastDescription;
                StringBuilder sb = new StringBuilder(String.valueOf(outputFile).length() + 56);
                sb.append("Test instrumentation process crashed. Check ");
                sb.append(outputFile);
                sb.append(" for details");
                orchestrationRunListener.testFailure(new ParcelableFailure(parcelableDescription, new Throwable(sb.toString())));
                orchestrationRunListener.testFinished(this.lastDescription);
            }
        }
    }

    public void handleNotification(Bundle bundle) {
        bundle.setClassLoader(getClass().getClassLoader());
        cacheStatus(bundle);
        for (OrchestrationRunListener orchestrationRunListener : this.listeners) {
            handleNotificationForListener(orchestrationRunListener, bundle);
        }
    }

    private void cacheStatus(Bundle bundle) {
        if (BundleJUnitUtils.getDescription(bundle) != null) {
            this.lastDescription = BundleJUnitUtils.getDescription(bundle);
        }
        int i = AnonymousClass1.$SwitchMap$androidx$test$orchestrator$listeners$OrchestrationListenerManager$TestEvent[TestEvent.valueOf(bundle.getString(KEY_TEST_EVENT)).ordinal()];
        if (i == 1) {
            this.markTerminationAsFailure = true;
        } else if (i == 2) {
            this.markTerminationAsFailure = false;
        } else if (i != 3) {
        } else {
            this.markTerminationAsFailure = false;
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: androidx.test.orchestrator.listeners.OrchestrationListenerManager$1  reason: invalid class name */
    /* loaded from: classes.dex */
    public static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$androidx$test$orchestrator$listeners$OrchestrationListenerManager$TestEvent;

        static {
            int[] iArr = new int[TestEvent.values().length];
            $SwitchMap$androidx$test$orchestrator$listeners$OrchestrationListenerManager$TestEvent = iArr;
            try {
                iArr[TestEvent.TEST_RUN_STARTED.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$androidx$test$orchestrator$listeners$OrchestrationListenerManager$TestEvent[TestEvent.TEST_FAILURE.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$androidx$test$orchestrator$listeners$OrchestrationListenerManager$TestEvent[TestEvent.TEST_RUN_FINISHED.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$androidx$test$orchestrator$listeners$OrchestrationListenerManager$TestEvent[TestEvent.TEST_STARTED.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                $SwitchMap$androidx$test$orchestrator$listeners$OrchestrationListenerManager$TestEvent[TestEvent.TEST_FINISHED.ordinal()] = 5;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                $SwitchMap$androidx$test$orchestrator$listeners$OrchestrationListenerManager$TestEvent[TestEvent.TEST_ASSUMPTION_FAILURE.ordinal()] = 6;
            } catch (NoSuchFieldError unused6) {
            }
            try {
                $SwitchMap$androidx$test$orchestrator$listeners$OrchestrationListenerManager$TestEvent[TestEvent.TEST_IGNORED.ordinal()] = 7;
            } catch (NoSuchFieldError unused7) {
            }
        }
    }

    private void handleNotificationForListener(OrchestrationRunListener listener, Bundle bundle) {
        switch (AnonymousClass1.$SwitchMap$androidx$test$orchestrator$listeners$OrchestrationListenerManager$TestEvent[TestEvent.valueOf(bundle.getString(KEY_TEST_EVENT)).ordinal()]) {
            case 1:
                listener.testRunStarted(BundleJUnitUtils.getDescription(bundle));
                return;
            case 2:
                listener.testFailure(BundleJUnitUtils.getFailure(bundle));
                return;
            case 3:
                listener.testRunFinished(BundleJUnitUtils.getResult(bundle));
                return;
            case 4:
                listener.testStarted(BundleJUnitUtils.getDescription(bundle));
                return;
            case 5:
                listener.testFinished(BundleJUnitUtils.getDescription(bundle));
                return;
            case 6:
                listener.testAssumptionFailure(BundleJUnitUtils.getFailure(bundle));
                return;
            case 7:
                listener.testIgnored(BundleJUnitUtils.getDescription(bundle));
                return;
            default:
                Log.e(TAG, "Unknown notification type");
                return;
        }
    }
}
